from rest_framework.response import Response
from rest_framework.views import APIView

from .models import User
from .ser import UserSer


class RegAPIView(APIView):
    def post(self, request):
        user = request.GET.get('user')
        password = request.GET.get('password')
        password1 = request.GET.get('password1')
        if password == password1:
            ser = UserSer(user, password)
            if ser.is_valid():
                ser.save()
                return Response({'msg': '注册成功', 'code': '200'})

    def get(self, request):
        user = request.GET.get('user')
        password = request.GET.get('password')
        user1 = User.objects.filter(user=user)
        password1 = User.objects.filter(password=password)
        if user == user1:
            if password == password1:
                return Response({'code': '200', 'msg': '登录成功'})


class Login(APIView):
    def get(self, request):
        data = User.objects.all()
        ser = UserSer(data=data, many=True)
        ser.is_valid()
        return Response(ser.data)
